package cn.dt70.services.impl;

import cn.dt70.dao.version.VersionMapper;
import cn.dt70.pojo.AppVersion;
import cn.dt70.services.VersionService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("versionService")
public class VersionServiceImpl implements VersionService {

    private static final Logger LOGGER = Logger.getLogger(VersionServiceImpl.class);

    @Resource
    private VersionMapper versionMapper;

    @Override
    public List<AppVersion> getAllVersion(int aId,int vId) {
        List<AppVersion> appVersionList = new ArrayList<>();

        try {
            appVersionList = versionMapper.qryAll(aId,vId);
            if(appVersionList.size() <= 0){
                LOGGER.debug("VersionServiceImpl.getAllVersion 查询失败。");
            }else {
                LOGGER.debug("VersionServiceImpl.getAllVersion 查询成功。");
                System.out.println(appVersionList.toString());
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("VersionServiceImpl.getAllVersion 异常："+e.getMessage());
        }

        return appVersionList;
    }

    @Override
    public boolean addNewVersion(AppVersion appVersion) {
            boolean flag = false;
        try {
            int row = versionMapper.save(appVersion);
            if(row != 1 ){
                LOGGER.debug("VersionServiceImpl.addNewVersion 保存版本信息失败！！！");
            }else {
                LOGGER.debug("VersionServiceImpl.addNewVersion 保存版本信息成功！！！");
                flag = true;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("VersionServiceImpl.addNewVersion 异常："+e.getMessage());
        }

        return flag;
    }

    @Override
    public boolean updateUser(AppVersion appVersion) {
        boolean flag = false;
        try {
            int row = versionMapper.modify(appVersion);
            if(row != 1 ){
                LOGGER.debug("VersionServiceImpl.updateUser 修改版本信息失败！！！");
            }else {
                LOGGER.debug("VersionServiceImpl.updateUser 修改版本信息成功！！！");
                flag = true;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("VersionServiceImpl.updateUser 异常："+e.getMessage());
        }

        return flag;
    }

    @Override
    public boolean removeUser(int id) {
        boolean flag = false;
        try {
            int row = versionMapper.del(id);
            if(row != 1 ){
                LOGGER.debug("VersionServiceImpl.removeUser 删除版本信息失败！！！");
            }else {
                LOGGER.debug("VersionServiceImpl.removeUser 删除版本信息成功！！！");
                flag = true;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("VersionServiceImpl.removeUser 异常："+e.getMessage());
        }

        return flag;
    }

    @Override
    public List<AppVersion> findVersionById(long infoId) {
        List<AppVersion> appVersionList = new ArrayList<>();
        try {
            appVersionList = versionMapper.getVersionByName(infoId);

            if (null == appVersionList){
                LOGGER.debug("VersionServiceImpl.findVersionByName 查询历史版本为空");
            }else {
                LOGGER.debug("VersionServiceImpl.findVersionByName 查询历史版本成功");
            }
        } catch (Exception e) {
            LOGGER.error("VersionServiceImpl.findVersionByName 异常："+e.getMessage());
        }
        return appVersionList;
    }
}
